//----------------------------------------------------------------------------------------------------------------------
// Copyright (c) 2012 James Whitworth
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the 'Software'), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//----------------------------------------------------------------------------------------------------------------------
#include "gmock/gmock.h"
#include "sqbind/sqbBind.h"
#include "../fixtures/SquirrelFixture.h"
#include "../mocks/autogen/MockFunction0.h"
//----------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------------
// THIS FILE IS AUTO GENERATED DO NOT EDIT.
//----------------------------------------------------------------------------------------------------------------------

namespace autogen
{
//----------------------------------------------------------------------------------------------------------------------
typedef SquirrelFixture BindFunctionTest;

//----------------------------------------------------------------------------------------------------------------------
typedef SquirrelFixture BindClassFunctionTest;

//----------------------------------------------------------------------------------------------------------------------
typedef SquirrelFixture BindSingletonFunctionTest;

//----------------------------------------------------------------------------------------------------------------------
// TestInvalidCall
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindFunctionTest, TestInvalidCall)
{
  EXPECT_FALSE(sqb::Bind::BindFunction(m_vm, 1, &MockFunction0::VoidFunction0, _SC("test")));

  sq_pushroottable(m_vm);
  EXPECT_FALSE(sqb::Bind::BindFunction(m_vm, 1, &MockFunction0::VoidFunction0, nullptr));
  EXPECT_FALSE(sqb::Bind::BindFunction(m_vm, 1, &MockFunction0::VoidFunction0, _SC("")));
  sq_poptop(m_vm);
}

//----------------------------------------------------------------------------------------------------------------------
// TestInvalidCall
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindClassFunctionTest, TestInvalidCall)
{
  EXPECT_FALSE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 1, &MockFunction0::ClassVoidFunction0, _SC("test")));

  sq_newclass(m_vm, SQFalse);
  EXPECT_FALSE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 1, &MockFunction0::ClassVoidFunction0, nullptr));
  EXPECT_FALSE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 1, &MockFunction0::ClassVoidFunction0, _SC("")));
  sq_poptop(m_vm);
}

//----------------------------------------------------------------------------------------------------------------------
// TestInvalidCall
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindSingletonFunctionTest, TestInvalidCall)
{
  MockFunction0 mock;
  EXPECT_FALSE(sqb::Bind::BindSingletonFunction(m_vm, 1, &mock, &MockFunction0::ClassVoidFunction0, _SC("test")));

  sq_newclass(m_vm, SQFalse);
  EXPECT_FALSE(sqb::Bind::BindSingletonFunction(m_vm, 1, &mock, &MockFunction0::ClassVoidFunction0, nullptr));
  EXPECT_FALSE(sqb::Bind::BindSingletonFunction(m_vm, 1, &mock, &MockFunction0::ClassVoidFunction0, _SC("")));
  EXPECT_FALSE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, 1, nullptr, &MockFunction0::ClassVoidFunction0, _SC("MockFunction0")));
  sq_poptop(m_vm);
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnVoid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindFunctionTest, Test0ParametersReturnVoid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  EXPECT_CALL(mock, ClassVoidFunction0())
    .Times(2);

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, -1, &MockFunction0::VoidFunction0, _SC("VoidFunction0")));

  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, 3, &MockFunction0::VoidFunction0, _SC("VoidFunction0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  CompileAndSucceedCall("VoidFunction0()");
  CompileAndSucceedCall("Mock.VoidFunction0()");
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnVoid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindClassFunctionTest, Test0ParametersReturnVoid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  EXPECT_CALL(mock, ClassVoidFunction0())
    .Times(1);
  EXPECT_CALL(mock, ConstClassVoidFunction0())
    .Times(1);

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_settypetag(m_vm, -1, sqb::ClassTypeTag<MockFunction0>::Get()));
  HSQOBJECT class_object;
  sq_getstackobj(m_vm, -1, &class_object);
  sqb::ClassTypeTag<MockFunction0>::Get()->SetClassObject(m_vm, class_object);
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, -1, &MockFunction0::ClassVoidFunction0, _SC("ClassVoidFunction0")));
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 2, &MockFunction0::ConstClassVoidFunction0, _SC("ConstClassVoidFunction0")));
  sq_poptop(m_vm);

  EXPECT_TRUE(sqb::Bind::BindVariable(m_vm, -1, static_cast<MockFunction0 *>(&mock), _SC("mock")));

  sq_poptop(m_vm);

  CompileAndSucceedCall("mock.ClassVoidFunction0()");
  CompileAndSucceedCall("mock.ConstClassVoidFunction0()");
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnVoid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindSingletonFunctionTest, Test0ParametersReturnVoid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  EXPECT_CALL(mock, ClassVoidFunction0())
    .Times(1);
  EXPECT_CALL(mock, ConstClassVoidFunction0())
    .Times(1);

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, -1, &mock, &MockFunction0::ClassVoidFunction0, _SC("ClassVoidFunction0")));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, 1, &mock, &MockFunction0::ConstClassVoidFunction0, _SC("ConstClassVoidFunction0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  CompileAndSucceedCall("Mock.ClassVoidFunction0()");
  CompileAndSucceedCall("ConstClassVoidFunction0()");
}

//----------------------------------------------------------------------------------------------------------------------
// Test0Parameters
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindFunctionTest, Test0Parameters)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  sq_pushroottable(m_vm);
  sqb::Bind::BindClass<BoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("BoundClass"));
  sqb::Bind::BindClass<OtherBoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("OtherBoundClass"));
  sq_poptop(m_vm);

  BoundClass expected_result(0xffffffff);
  EXPECT_CALL(mock, ClassFunction0())
    .Times(2)
    .WillRepeatedly(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, -1, &MockFunction0::Function0, _SC("Function0")));

  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, 3, &MockFunction0::Function0, _SC("Function0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  BoundClass actual_result = CompileAndCallReturnResult<BoundClass>("return Function0()");
  EXPECT_EQ(expected_result, actual_result);
  actual_result = CompileAndCallReturnResult<BoundClass>("return Mock.Function0()");
  EXPECT_EQ(expected_result, actual_result);
}

//----------------------------------------------------------------------------------------------------------------------
// Test0Parameters
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindClassFunctionTest, Test0Parameters)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  sq_pushroottable(m_vm);
  sqb::Bind::BindClass<BoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("BoundClass"));
  sqb::Bind::BindClass<OtherBoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("OtherBoundClass"));
  sq_poptop(m_vm);

  BoundClass expected_result(0xffffffff);
  EXPECT_CALL(mock, ClassFunction0())
    .WillOnce(::testing::Return(expected_result));
  EXPECT_CALL(mock, ConstClassFunction0())
    .WillOnce(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_settypetag(m_vm, -1, sqb::ClassTypeTag<MockFunction0>::Get()));
  HSQOBJECT class_object;
  sq_getstackobj(m_vm, -1, &class_object);
  sqb::ClassTypeTag<MockFunction0>::Get()->SetClassObject(m_vm, class_object);
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, -1, &MockFunction0::ClassFunction0, _SC("ClassFunction0")));
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 2, &MockFunction0::ConstClassFunction0, _SC("ConstClassFunction0")));
  sq_poptop(m_vm);

  EXPECT_TRUE(sqb::Bind::BindVariable(m_vm, -1, static_cast<MockFunction0 *>(&mock), _SC("mock")));

  sq_poptop(m_vm);

  BoundClass actual_result = CompileAndCallReturnResult<BoundClass>("return mock.ClassFunction0()");
  EXPECT_EQ(expected_result, actual_result);
  actual_result = CompileAndCallReturnResult<BoundClass>("return mock.ConstClassFunction0()");
  EXPECT_EQ(expected_result, actual_result);
}

//----------------------------------------------------------------------------------------------------------------------
// Test0Parameters
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindSingletonFunctionTest, Test0Parameters)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  sq_pushroottable(m_vm);
  sqb::Bind::BindClass<BoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("BoundClass"));
  sqb::Bind::BindClass<OtherBoundClass, sqb::NoBaseClass>(m_vm, -1, _SC("OtherBoundClass"));
  sq_poptop(m_vm);

  BoundClass expected_result(0xffffffff);
  EXPECT_CALL(mock, ClassFunction0())
    .WillOnce(::testing::Return(expected_result));
  EXPECT_CALL(mock, ConstClassFunction0())
    .WillOnce(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, -1, &mock, &MockFunction0::ClassFunction0, _SC("ClassFunction0")));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, 1, &mock, &MockFunction0::ConstClassFunction0, _SC("ConstClassFunction0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  BoundClass actual_result = CompileAndCallReturnResult<BoundClass>("return Mock.ClassFunction0()");
  EXPECT_EQ(expected_result, actual_result);
  actual_result = CompileAndCallReturnResult<BoundClass>("return ConstClassFunction0()");
  EXPECT_EQ(expected_result, actual_result);
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnInvalid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindFunctionTest, Test0ParametersReturnInvalid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  InvalidStackUtilsClass expected_result;
  EXPECT_CALL(mock, ClassInvalidFunction0())
    .Times(2)
    .WillRepeatedly(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, -1, &MockFunction0::InvalidFunction0, _SC("InvalidFunction0")));

  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindFunction(m_vm, 3, &MockFunction0::InvalidFunction0, _SC("InvalidFunction0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  CompileAndFailCall("InvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
  CompileAndFailCall("Mock.InvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnInvalid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindClassFunctionTest, Test0ParametersReturnInvalid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  InvalidStackUtilsClass expected_result;
  EXPECT_CALL(mock, ClassInvalidFunction0())
    .WillOnce(::testing::Return(expected_result));
  EXPECT_CALL(mock, ConstClassInvalidFunction0())
    .WillOnce(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_settypetag(m_vm, -1, sqb::ClassTypeTag<MockFunction0>::Get()));
  HSQOBJECT class_object;
  sq_getstackobj(m_vm, -1, &class_object);
  sqb::ClassTypeTag<MockFunction0>::Get()->SetClassObject(m_vm, class_object);
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, -1, &MockFunction0::ClassInvalidFunction0, _SC("ClassInvalidFunction0")));
  EXPECT_TRUE(sqb::Bind::BindClassFunction<MockFunction0>(m_vm, 2, &MockFunction0::ConstClassInvalidFunction0, _SC("ConstClassInvalidFunction0")));
  sq_poptop(m_vm);

  EXPECT_TRUE(sqb::Bind::BindVariable(m_vm, -1, static_cast<MockFunction0 *>(&mock), _SC("mock")));

  sq_poptop(m_vm);

  CompileAndFailCall("mock.ClassInvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
  CompileAndFailCall("mock.ConstClassInvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
}

//----------------------------------------------------------------------------------------------------------------------
// Test0ParametersReturnInvalid
//----------------------------------------------------------------------------------------------------------------------
TEST_F(BindSingletonFunctionTest, Test0ParametersReturnInvalid)
{
  ::testing::StrictMock<MockFunction0> mock;
  MockFunction0::m_instance = &mock;

  InvalidStackUtilsClass expected_result;
  EXPECT_CALL(mock, ClassInvalidFunction0())
    .WillOnce(::testing::Return(expected_result));
  EXPECT_CALL(mock, ConstClassInvalidFunction0())
    .WillOnce(::testing::Return(expected_result));

  // bind the objects required for calling the function
  //
  sq_pushroottable(m_vm);

  // bind the mock class
  //
  sq_pushstring(m_vm, _SC("Mock"), -1);
  EXPECT_SQ_SUCCEEDED(m_vm, sq_newclass(m_vm, SQFalse));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, -1, &mock, &MockFunction0::ClassInvalidFunction0, _SC("ClassInvalidFunction0")));
  EXPECT_TRUE(sqb::Bind::BindSingletonFunction<MockFunction0>(m_vm, 1, &mock, &MockFunction0::ConstClassInvalidFunction0, _SC("ConstClassInvalidFunction0")));
  EXPECT_SQ_SUCCEEDED(m_vm, sq_rawset(m_vm, -3));

  sq_poptop(m_vm);

  CompileAndFailCall("Mock.ClassInvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
  CompileAndFailCall("ConstClassInvalidFunction0()");
  CheckErrorString("error returning object of type 'InvalidStackUtilsClass'");
}

} // namespace autogen
